package com.tengju.data.domain.shared;

import lombok.Getter;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.Objects;

/**
 * @author chenhang
 * @Date 2021/3/18
 */
@Getter
public class Week {

    LocalDate startDate;
    LocalDate endDate;
    LocalDate randomDate;

    public Week(LocalDate randomDate) {
        if (randomDate == null) {
            return;
        }
        this.randomDate = randomDate;

        DayOfWeek dayOfWeek = randomDate.getDayOfWeek();

        this.startDate = randomDate.minusDays(dayOfWeek.getValue() - 1);
        this.endDate = randomDate.plusDays(7 - dayOfWeek.getValue());
    }


    public boolean sameWeek(LocalDate date) {
        return startDate.compareTo(date) <=0 && endDate.compareTo(date) >= 0;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Week week = (Week) o;
        return Objects.equals(startDate, week.startDate) && Objects.equals(endDate, week.endDate);
    }

    @Override
    public int hashCode() {
        return Objects.hash(startDate, endDate);
    }
}
